package chess;

import java.awt.*;
import java.util.*;

@SuppressWarnings("serial")
public class Pawn extends ChessPiece
{
	private boolean doubleMove;
	
	public Pawn(boolean isWhite, Location loc)
	{
		super(isWhite, loc);
	}
	
	public void draw(Graphics g)
	{
		Toolkit toolkit = Toolkit.getDefaultToolkit( );
		 Image v2_act = toolkit.getImage("1pawn.gif" );
		  Image v3_act = toolkit.getImage("0pawn.gif" );
		if(getColor())
		{
			 g.drawImage(v2_act,10,10,null);
		}
		else
		{
			 g.drawImage(v3_act,10,10,null);
		}
		
	}
	
	public boolean getDoubleMove()
	{
		return doubleMove;
	}
	
	public void setDoubleMove(boolean input)
	{
		doubleMove=input;
	}
	
	public ArrayList<Location> getMoves(BoardState board)
	{
		ArrayList<Location> possibleMoves = new ArrayList<Location>();
		int y = getLocation().getRow();
		int x = getLocation().getCol();
		
		Location whiteOne = new Location(y-1,x);
		Location whiteTwo = new Location(y-2,x);
		Location whiteLeft = new Location(y-1,x-1);
		Location whiteRight = new Location(y-1,x+1);
		
		Location blackOne = new Location(y+1,x);
		Location blackTwo = new Location(y+2,x);
		Location blackLeft = new Location(y+1,x-1);
		Location blackRight = new Location(y+1,x+1);
		
		if(getColor())
		{
			if(y!=0)
			{
				if(board.isValid(whiteOne) && board.isEmpty(whiteOne))
					possibleMoves.add(whiteOne);
				if(board.isValid(whiteTwo) && getLocation().getRow()==6 && board.isEmpty(whiteTwo) && board.isEmpty(whiteOne))
					possibleMoves.add(whiteTwo);
				if(board.isValid(whiteLeft) && !board.isEmpty(whiteLeft) && !board.isPieceWhite(whiteLeft))
					possibleMoves.add(whiteLeft);
				if(board.isValid(whiteRight) && !board.isEmpty(whiteRight) && !board.isPieceWhite(whiteRight))
					possibleMoves.add(whiteRight);
			}
		}
		else
		{
			if(y!=7)
			{
				if(board.isValid(blackOne) && board.isEmpty(blackOne))
					possibleMoves.add(blackOne);
				if(board.isValid(blackTwo) && getLocation().getRow()==1 && board.isEmpty(blackTwo) && board.isEmpty(blackOne))
					possibleMoves.add(blackTwo);
				if(board.isValid(blackLeft) && !board.isEmpty(blackLeft) && board.isPieceWhite(blackLeft))
					possibleMoves.add(blackLeft);
				if(board.isValid(blackRight) && !board.isEmpty(blackRight) && board.isPieceWhite(blackRight))
					possibleMoves.add(blackRight);
			}
		}
		return possibleMoves;
	}
	
	public void moveTo(Location moveLoc)
	{
		if((int)Math.abs(getLocation().getRow()-moveLoc.getRow()) > 1)
			doubleMove = true;
		else
			doubleMove = false;
		setLocation(moveLoc);
	}
	
	public String toString()
	{
		return super.toString()+" Pawn";
	}
}
